﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ES_Project.Model;
using System.Data;
using System.Data.SqlClient;

using System.Collections;
using System.Web.Security;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Threading;
using System.Globalization;
using System.Xml.Linq;

namespace ES_Project.Pages
{
    public partial class WebForm8 : System.Web.UI.Page
    {
        string StrConn = WebConfigurationManager.ConnectionStrings["baseessystem"].ToString();
        MySqlCommand objCmd = new MySqlCommand(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dataclsregis();
                clsRegId.Text = Request.QueryString["clsRegId"];
                if (!clsRegId.Text.Equals(""))
                {
                    clsRegId.Text = Request.QueryString["clsRegId"];
                    CMD_INSERT.Enabled = false;
                    CMD_EDIT.Enabled = true;
                    Editing();
                }
                else
                {
                    CMD_INSERT.Enabled = true;
                    CMD_EDIT.Enabled = false;
                }
            }
        }
        protected void CMD_CLEAR_Click(object sender, EventArgs e)
        {
            clsRegCode.Text = "";
            clsName.Text = "";
            level.Text = "";
            CMD_INSERT.Enabled = true;
            CMD_EDIT.Enabled = false;
        }
        void dataclsregis()
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            String strSQL;
            strSQL = "SELECT * FROM cls_register_mast  ";
            MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "cls_register_mast");
            GridView4.DataSource = ds.Tables["cls_register_mast"];
            GridView4.DataBind();
        }
        protected void CMD_INSERT_Click(object sender, EventArgs e)
        {
            using (ES_Project.Model.essystemEntities context = new ES_Project.Model.essystemEntities())
            {
                Button bt =  (Button)sender;
                switch (bt.ID) 
                {
                    case "CMD_INSERT":
                        List<cls_register_mast> sm = context.cls_register_mast.Where("it.[cls_reg_code]='" + clsRegCode.Text + "' ").ToList();
                         if (sm.Count == 0)
                         {
                             cls_register_mast crm = new cls_register_mast();
                             crm.cls_reg_code = clsRegCode.Text;
                             crm.cls_name = clsName.Text;
                             crm.level = level.Text;
                             context.cls_register_mast.AddObject(crm);
                             if (context.SaveChanges() > 0)
                             {
                                 ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert1", "alert('บันทึกเรียบร้อยแล้ว')", true);
                                 GridView4.DataBind();
                                 GridView4.SelectedIndex = -1;
                                 clsRegCode.Text = "";
                                 clsName.Text = "";
                                 level.Text = "";
                             }
                         }
                         else
                         {
                             ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ReturnMessini9", "alert('รหัสนักเรียนซ้ำ');", true);

                         }
                        break;
                    case "CMD_EDIT":
                        List<cls_register_mast> crm2 = context.cls_register_mast.Where("it.[cls_reg_id]="+clsRegId.Text+" ").ToList();
                        if (crm2.Count() == 1)
                        {
                            cls_register_mast crmEdit = context.cls_register_mast.Where("it.[cls_reg_id]=" + clsRegId.Text + " ").First();
                            if (crmEdit != null)
                            {
                                crmEdit.cls_reg_code = clsRegCode.Text;
                                crmEdit.cls_name = clsName.Text;
                                crmEdit.level = level.Text;

                                if (context.SaveChanges() > 0)
                                {

                                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert2", "alert('แก้ไขข้อมูลเรียบร้อยแล้ว')", true);
                                    GridView4.DataBind();
                                    GridView4.SelectedIndex = -1;
                                    CMD_INSERT.Enabled = true;
                                    CMD_EDIT.Enabled = false;
                                    clsRegCode.Text = "";
                                    clsName.Text = "";
                                    level.Text = "";
                                }
                            }
                        }
                        break;
                }
            }
            dataclsregis();
        }
        protected void GridView4_SelectedIndexChanged(object sender, EventArgs e)
        {
            CMD_INSERT.Enabled = false;
            CMD_EDIT.Enabled = true;
            using (essystemEntities context = new essystemEntities())
            {
                List<cls_register_mast> crm = context.cls_register_mast.Where("it.[cls_reg_id] = " + ((HiddenField)GridView4.SelectedRow.Cells[1].FindControl("HiddenField4")).Value.ToString() + "").ToList();
                if (crm.Count > 0) 
                {
                    clsRegId.Text = crm[0].cls_reg_id.ToString();
                    clsRegCode.Text = crm[0].cls_reg_code;
                    clsName.Text = crm[0].cls_name;
                    level.Text = crm[0].level;
                }
            }
        }
         void Editing()
        {
            //CMD_INSERT.Enabled = false;
            //CMD_EDIT.Enabled = true;
            using (essystemEntities context = new essystemEntities())
            {
                List<cls_register_mast> crm = context.cls_register_mast.Where("it.[cls_reg_id] = " + clsRegId.Text + "").ToList();
                if (crm.Count > 0)
                {
                    clsRegId.Text = crm[0].cls_reg_id.ToString();
                    clsRegCode.Text = crm[0].cls_reg_code;
                    clsName.Text = crm[0].cls_name;
                    level.Text = crm[0].level;
                }
            }
        }
        protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{

            //    ImageButton ib = (ImageButton)e.Row.FindControl("ImageButton1");
            //    ib.Attributes.Add("onclick", "return confirm('คุณต้องการที่จะลบข้อมูล?');");

            //}
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // loop all data rows
                foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    // check all cells in one row
                    foreach (Control control in cell.Controls)
                    {
                        // Must use LinkButton here instead of ImageButton
                        // if you are having Links (not images) as the command button.
                        ImageButton button = control as ImageButton;
                        if (button != null && button.CommandName == "Delete")
                            // Add delete confirmation
                            button.OnClientClick = "if (!confirm('คุณต้องการที่จะลบข้อมูล?')) return;";
                    }
                }
            }
        }

        protected void GridView4_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            

            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            strSQL = "DELETE FROM cls_register_mast WHERE cls_reg_id ='" + GridView4.DataKeys[e.RowIndex].Value + "'";
            Conn.Open();
            objCmd = new MySqlCommand(strSQL, Conn);

            objCmd.ExecuteNonQuery();



            GridView4.EditIndex = -1;
            dataclsregis();
        }
    }
}